<template>
  <div>
    <el-dialog
      title="新增员工"
      :visible.sync="showDialog"
      :before-close="dialogVisible"
    >
      <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择日期" />
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
            <!-- 遍历只能遍历组件的数据 -->
            <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="getDepartments" />
          <div v-if="showTree" class="treeWrapper">
            <el-tree
              v-loading="loading"
              class="treeDeparment"
              :data="treeData"
              :props="{ label: 'name' }"
              :default-expand-all="true"
              @node-click="selectNode"
            />
          </div>
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择日期" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { addEmployee } from '@/api/employees'
import { getDepartments } from '@/api/departments'
import EmployeeEnum from '@/api/constant/employees'
import { convertTreeData } from '@/utils'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const checkTime = (rule, value, callback) => {
      if (!this.formData.timeOfEntry || !this.formData.correctionTime) {
        console.log('未填写时间')
        callback()
      } else {
        const timeOfEntrys = this.formData.timeOfEntry.getTime()
        const correctionTimes = this.formData.correctionTime.getTime()
        console.log(timeOfEntrys, '入职时间')
        console.log(correctionTimes, '转正时间')
        timeOfEntrys < correctionTimes ? callback() : callback(new Error('入职前不能转正'))
        callback()
      }
    }
    return {
      EmployeeEnum,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      treeData: [], // 定义数组接收树形数据
      showTree: false, // 控制树形的显示或者隐藏
      loading: true,
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 50, message: '用户姓名为1-4位'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }, { trigger: 'change', validator: checkTime }, { trigger: 'blur', validator: checkTime }],
        correctionTime: [{ trigger: 'change', validator: checkTime }, { trigger: 'blur', validator: checkTime }]
      }
    }
  },
  methods: {
    async btnOk() {
      try {
        await this.$refs.form.validate()
        await addEmployee(this.formData)
        await this.$message.success('添加成功')
        this.$emit('update:showDialog', false)
        this.$emit('addUser')
      } catch (error) {
        console.log(error)
      }
    },
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      console.log(depts)
      this.treeData = convertTreeData(depts, '')
      this.loading = false
    },
    selectNode(node) {
      console.log(node)
      this.formData.departmentName = node.name
      this.showTree = false
    },
    dialogVisible() {
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.showTree = false
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    }

  }
}
</script>

<style lang="scss" scoped>
.treeWrapper {
  margin-top: 2px;
  width: 300px;
  height: 300px;
  border: 1px solid #888;
  overflow: hidden;
  position: absolute;
  left: 0;
  z-index: 999;
  .treeDeparment {
    width: 317px;
    height: 100%;
    overflow-y: scroll;
  }
}
</style>
